package com.fxf.srb.core.controller;


import com.alibaba.fastjson.JSON;
import com.fxf.common.result.R;
import com.fxf.srb.base.util.JwtUtils;
import com.fxf.srb.core.hfb.RequestHelper;
import com.fxf.srb.core.service.IUserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author fxf
 * @since 2021-05-26
 */
@Slf4j
@Api(tags = "会员账号")
@RestController
@RequestMapping("/api/core/userAccount")
public class UserAccountController {


    @Resource
    private IUserAccountService iUserAccountService;

    @ApiOperation(value = "用户充值")
    @PostMapping("/auth/commitCharge/{chargeAmt}")
    public R commitCharge(
            @ApiParam(value = "充值金额",required = true)
            @PathVariable BigDecimal chargeAmt, HttpServletRequest request){

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        String formStr = iUserAccountService.commitCharge(chargeAmt,userId);
        return R.ok().data("formStr",formStr);

    }

    @ApiOperation(value = "用户充值异步回调")
    @PostMapping("/notify")
    public String notify(HttpServletRequest request){
        Map<String, Object> map = RequestHelper.switchMap(request.getParameterMap());

        if (RequestHelper.isSignEquals(map)){
            if("0001".equals(map.get("resultCode"))) {
                return iUserAccountService.notify(map);
            }else {
                return "success";
            }

        }else {
            return "fail";
        }

    }

    @ApiOperation(value = "查询用户的账户余额")
    @GetMapping("/auth/getAccount")
    public R getAccount(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        BigDecimal bigDecimal =  iUserAccountService.getAccount(userId);
        return R.ok().data("account",bigDecimal);
    }

    @ApiOperation("用户提现")
    @PostMapping("/auth/commitWithdraw/{fetchAmt}")
    public R commitWithdraw(
            @ApiParam(value = "金额", required = true)
            @PathVariable BigDecimal fetchAmt, HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        String formStr = iUserAccountService.commitWithdraw(fetchAmt, userId);
        return R.ok().data("formStr", formStr);
    }

    @ApiOperation("用户提现异步回调")
    @PostMapping("/notifyWithdraw")
    public String notifyWithdraw(HttpServletRequest request) {

        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        if (RequestHelper.isSignEquals(paramMap)){
            if ("0001".equals(paramMap.get("result_code"))){
                iUserAccountService.notifyWithdraw(paramMap);
            }else {
                log.info("提现异步回调充值失败：" + JSON.toJSONString(paramMap));
                return "fail";
            }
        }else {
            log.info("提现异步回调签名错误：" + JSON.toJSONString(paramMap));
            return "fail";
        }
        return "success";

    }
}

